package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.search.domain.DestinationEs;
import cn.wolfcode.wolf2w.search.domain.StrategyEs;
import cn.wolfcode.wolf2w.search.domain.TravelEs;
import cn.wolfcode.wolf2w.search.domain.UserInfoEs;
import cn.wolfcode.wolf2w.search.query.SearchQueryObject;
import cn.wolfcode.wolf2w.search.service.ISearchService;
import cn.wolfcode.wolf2w.search.vo.SearchResultVO;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SearchController {

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private ITravelService travelService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private ISearchService searchService;

    @RequestMapping("/q")
    public Object search(SearchQueryObject qo) throws UnsupportedEncodingException {

        String decode = URLDecoder.decode(qo.getKeyword(), "UTF-8");
        qo.setKeyword(decode);

        switch (qo.getType()) {
            case SearchQueryObject.TYPE_DEST :
                return searchDest(qo);
            case SearchQueryObject.TYPE_STRATEGY :
                return searchStrategy(qo);
            case SearchQueryObject.TYPE_TRAVEL :
                return searchTravel(qo);
            case SearchQueryObject.TYPE_USER :
                return searchUser(qo);
            default:
                return searchAll(qo);
        }
    }

    //  全部
    private Object searchAll(SearchQueryObject qo) {
        SearchResultVO searchResultVO = new SearchResultVO();

        long total = 0L;

        Page<UserInfo> us = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo, "city", "info");
        searchResultVO.setUsers(us.getContent());
        total += us.getTotalElements();

        Page<Travel> ts = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class, qo, "title", "summary");
        searchResultVO.setTravels(ts.getContent());
        total += ts.getTotalElements();

        Page<Strategy> sts = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo, "title", "subTitle", "summary");
        searchResultVO.setStrategys(sts.getContent());
        total += sts.getTotalElements();

        Page<Destination> ds = searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME, Destination.class, qo, "name", "info");
        searchResultVO.setDests(ds.getContent());
        total += ds.getTotalElements();

        searchResultVO.setTotal(total);

        Map<String, Object> map = new HashMap<>();
        map.put("qo", qo);
        map.put("result", searchResultVO);

        return JsonResult.success(map);
    }

    //  用户
    private Object searchUser(SearchQueryObject qo) {

        Page<UserInfo> page = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo, "city", "info");

        Map<String, Object> map = new HashMap<>();
        map.put("qo", qo);
        map.put("page", page);

        return JsonResult.success(map);
    }

    //  游记
    private Object searchTravel(SearchQueryObject qo) {

        Page<Travel> page = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class, qo, "title", "summary");

        Map<String, Object> map = new HashMap<>();
        map.put("qo", qo);
        map.put("page", page);

        return JsonResult.success(map);
    }

    //  攻略
    private Object searchStrategy(SearchQueryObject qo) {

        Page<Strategy> page = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo, "title", "subTitle", "summary");

        Map<String, Object> map = new HashMap<>();
        map.put("qo", qo);
        map.put("page", page);

        return JsonResult.success(map);

    }

    //  目的地
    private Object searchDest(SearchQueryObject qo) {

        Destination destination = destinationService.queryName(qo.getKeyword());

        SearchResultVO searchResultVO = new SearchResultVO();

        if (destination != null) {
            List<Strategy> strategyList = strategyService.queryDestName(qo.getKeyword());
            searchResultVO.setStrategys(strategyList);

            List<Travel> travelList = travelService.queryDestName(qo.getKeyword());
            searchResultVO.setTravels(travelList);

            List<UserInfo> userInfoList = userInfoService.queryCity(qo.getKeyword());
            searchResultVO.setUsers(userInfoList);

            long total = strategyList.size() + travelList.size() + userInfoList.size();
            searchResultVO.setTotal(total);

        }

        Map<String, Object> map = new HashMap<>();
        map.put("result", searchResultVO);
        map.put("qo", qo);
        map.put("dest", destination);

        return JsonResult.success(map);
    }
}
